<?php
/**
 * WpFilterer.php
 * 
 * This file contains the static class WpFilterer 
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage base
 */


/**
 * A class to filter values
 * 
 * The WpFilterer class provides functions to filter/sanitize values
 * 
 * Example:
 * <code>
 * echo WPFilterer::filter('striptag', '<b>This is bold</b>');
 * //Prints:
 * //This is bold
 * </code>
 * 
 * The list of valid filters
 * <ul>
 *  <li>striptag - A wrapper for the php-function</li>
 *  <li>addslash - A wrapper for the php-function</li>
 *  <li>blocktags - Instead of removing the tags, it converts it to html special chars</li>
 *  <li>html2txt - Similar to striptags, but it removes whole style blocks and script blocks</li>
 *  <li>htmlspecialchars - A wrapper for the php-function</li>
 *  <li>htmlspecialchars_decode - A wrapper for the php-function</li>
 *  <li>htmlentities - A wrapper for the php-function</li>
 *  <li>nl2br - A wrapper for the php-function</li>
 * </ul>
 * 
 * To use other filters, create an own filter class and inherit from WpFilterer. 
 * 
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage base
 */
class WpFilterer
{
	/**
	 * Filters the value
	 * @param string $filter A valid filter
	 * @param mixed $value The value to filter
	 * @return The filtered value
	 */
	public static function filter($filter='', $value)
	{
		switch($filter) {
			case 'striptag': return strip_tags($value); //Removes (html) tags from strings
			case 'addslash': return addslashes($value); //Adds slashes to avoid SqlInjection
      case 'blocktags': return str_ireplace('<', '&lt', str_ireplace('>', '&gt', $value)); //Replaces < and > with html entities
      case 'html2txt': 
          $search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript 
               '@<[\/\!]*?[^<>]*?>@si',                     // Strip out HTML tags 
               '@<style[^>]*?>.*?</style>@siU',             // Strip style tags properly 
               '@<![\s\S]*?--[ \t\n\r]*>@'                  // Strip multi-line comments including CDATA 
          ); 
          return preg_replace($search, '', $value);
      case 'htmlspecialchars': return htmlspecialchars($value);
      case 'htmlspecialchars_decode': return htmlspecialchars_decode($value);
      case 'htmlentities': return htmlentities($value);
      case 'nl2br': return nl2br($value);
		}
		
		return $value;
	}
}
/* End of file WpFilterer.php */
/* Location: ./wildpack/base/WpFilterer.php */ 